
using System.Collections;
using MyFrame.Pool;
using UnityEngine;

namespace Platform.Skill.Deployer
{
    public class CloneSkillDeployer : SkillDeployer
    {
        private float cloneTimer;

        [Header("Collision info")]
        public Transform attackCheck;
        public float atatckCheckRadius = 0.2f;

        public float colorLossingSpeed = 1f;

        public bool canCloneAtCounter = true;
        private float cloneDelay = 0.5f;

        private bool canCopyClone = true;

        private Transform closestEnemy;

        private int facingDir = 1;

        protected void Awake()
        {
            sr = GetComponentInChildren<SpriteRenderer>();
            anim = GetComponentInChildren<Animator>();
        }

        void OnEnable()
        {

        }
        void Update()
        {
            cloneTimer -= Time.deltaTime;

            if (cloneTimer < 0)
            {
                sr.color = new Color(1, 1, 1, sr.color.a - colorLossingSpeed * Time.deltaTime);

                if (sr.color.a <= 0)
                {
                    PoolMgr.Instance.Release(this.gameObject);
                    EndEffect();
                }
            }
        }

        public override void DeploySkill()
        {
            if (canCloneAtCounter && OwnerEntity.CurrentState == Entity.States.CounterAttack)
            {
                sr.color = new Color(1, 1, 1, 0);
                cloneTimer = skillData.durationTime + cloneDelay;
                StartCoroutine(CreateCloneDelay());
            }
            else
            {
                cloneTimer = skillData.durationTime;
                GreateClone();
            }
        }
        IEnumerator CreateCloneDelay()
        {
            yield return new WaitForSeconds(cloneDelay);
            GreateClone();
        }

        private void GreateClone()
        {
            sr.color = new Color(1, 1, 1, 1);
            //固定方向，方便转向
            skillData.skillPrefab.transform.rotation = Quaternion.identity;
            FaceClosestTarget();
            ImpactTargets();
        }

        private void FaceClosestTarget()
        {
            Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 25);
            float closestDistance = Mathf.Infinity;

            foreach (Collider2D hit in colliders)
            {
                if (hit.TryGetComponent<Enemy>(out Enemy entity))
                {
                    float distanceToEnemy = Vector2.Distance(transform.position, entity.transform.position);

                    if (distanceToEnemy < closestDistance)
                    {
                        closestDistance = distanceToEnemy;
                        closestEnemy = entity.transform;
                    }
                }
            }

            if (closestEnemy != null)
            {
                if (closestEnemy.position.x < transform.position.x)
                {
                    facingDir = -1;
                    transform.Rotate(0, 180, 0);
                }
            }
        }


        #region  动画
        private void AnimationTrigger()
        {
            cloneTimer = -.1f;
        }

        private void AttackTrigger()
        {
            Collider2D[] colliders = Physics2D.OverlapCircleAll(attackCheck.position, atatckCheckRadius);

            foreach (Collider2D hit in colliders)
            {
                if (hit.TryGetComponent<Enemy>(out Enemy entity))
                {
                    AttackStart(hit);

                    if (canCopyClone)
                    {
                        if (UnityEngine.Random.value < skillData.chanceActive)
                        {
                            var targetPos = skillData.targetPos == default ? OwnerPos : skillData.targetPos;
                            OwnerEntity.UseSkill(SkillEnum.Clone, new Vector3(targetPos.x + 1f * facingDir, targetPos.y, 0));
                        }
                    }
                }
            }
        }
    }
    #endregion
}